﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UnlessComment.Model;
using Web4Comment.Service;

namespace Web4Comment
{
    public partial class register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] != null && Session["user_id"] != null)
            {
                Response.Redirect("./profile.aspx?user=" + Session["username"]);
            }

            if (!IsPostBack)
            {
                usernameCheckLabel.Text = "请输入6~20位用户名";
                passwordCheckLabel.Text = "请输入6~20位密码";
                repeatPasswordCheckLabel.Text = "请重复密码";
                emailCheckLabel.Text = "请输入常用email";
            }
        }

        protected void registerButton_Click(object sender, EventArgs e)
        {
            if (usernameCheckLabel.Text != "该用户名可以使用" || passwordCheckLabel.Text != ""
                || repeatPasswordCheckLabel.Text != "" || emailCheckLabel.Text != "该邮箱可以使用")
            {
                Response.Write("<script language=javascript>alert(\"" + "注册失败！请根据提示修改" + "\");</script>");
                return;
            }

            UnlessComment.Model.User user = new UnlessComment.Model.User();

            user.username = usernameTextBox.Text.Trim();
            user.password = passwordTextBox.Text.Trim();
            user.email = emailTextBox.Text.Trim();

            UnlessComment.Model.UserInfo userinfo = new UnlessComment.Model.UserInfo();
            userinfo.sex = maleRadioButton.Checked ? "1" : "0";
            userinfo.location = locationTextBox.Text.Trim();
            userinfo.screen_name = hoppyTextBox.Text.Trim();

            //userService = new UserService();
            int exist = Service.UserService.register(user, userinfo);
            if (exist < 0)
            {
                Response.Write("<script language=javascript>alert(\"" + "注册失败！请重新尝试" + "\");</script>");
                return;
            }
            Response.Write("<script language=javascript>alert(\"" + "注册成功！" + "\");</script>");
            Response.Redirect("./logon.aspx");
            
        }

        protected void usernameTextBox_TextChanged(object sender, EventArgs e)
        {
            if (usernameTextBox.Text == null || usernameTextBox.Text.Trim() == "")
            {
                usernameCheckLabel.Text = "用户名不能为空";
            }
            else
            {
                //userService = new UserService();
                if (usernameTextBox.Text.Trim().Length < 6)
                {
                    usernameCheckLabel.Text = "该用户名不能少于6位";
                }
                else if (usernameTextBox.Text.Trim().Length > 20)
                {
                    usernameCheckLabel.Text = "该用户名不能大于20位";
                }
                else if (Service.UserService.existUsername(usernameTextBox.Text.Trim()) != 0)
                {
                    usernameCheckLabel.Text = "该用户名已存在";
                }
                else
                {
                    usernameCheckLabel.Text = "该用户名可以使用";
                }
            }
        }

        protected void passwordTextBox_TextChanged(object sender, EventArgs e)
        {
            if (passwordTextBox.Text == null || passwordTextBox.Text.Trim() == "")
            {
                passwordCheckLabel.Text = "密码不能为空";
            }
            else
            {
                if (passwordTextBox.Text.Trim().Length < 6)
                {
                    passwordCheckLabel.Text = "密码不能少于6位";
                }
                else if (passwordTextBox.Text.Trim().Length > 20)
                {
                    passwordCheckLabel.Text = "密码不能多于20位";
                }
                else if(passwordTextBox.Text.Trim() == usernameTextBox.Text.Trim())
                {
                    passwordCheckLabel.Text = "用户名密码不能相同";
                }
                else
                {
                    passwordCheckLabel.Text = "";
                }
            }
        }


        protected void repeatPasswordTextBox_TextChanged(object sender, EventArgs e)
        {
            if (repeatPasswordTextBox.Text.Trim() != passwordTextBox.Text.Trim())
            {
                repeatPasswordCheckLabel.Text = "两次密码输入不一致";
            }
            else
            {
                repeatPasswordCheckLabel.Text = "";
            }
        }

        protected void emailTextBox_TextChanged(object sender, EventArgs e)
        {
            if (emailTextBox.Text == null || emailTextBox.Text.Trim() == "")
            {
                emailCheckLabel.Text = "邮箱不能为空";
            }
            else
            {
                //userService = new UserService();
                if (Service.UserService.existEmail(emailTextBox.Text.Trim()) != 0)
                {
                    emailCheckLabel.Text = "该邮箱已存在";
                }
                else
                {
                    emailCheckLabel.Text = "该邮箱可以使用";
                }
            }
        }

        protected void noticeCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (noticeCheckBox.Checked)
            {
                checkboxLabel.Visible = false;
                registerButton.Enabled = true;
            }
            else
            {
                checkboxLabel.Visible = true;
                registerButton.Enabled = false;
            }
        }

    }
}